/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ca.etsmtl.log540.tp3;

import static ca.etsmtl.log540.tp3.RepeatingClient.numberOfRepetitions;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 *
 * @author SDION
 */
public class UTFClient {

    final String UTF8 = "UTF8";

    protected static int port;
    protected static Socket socket;

    protected static OutputStreamWriter writer;
    protected static BufferedReader reader;

    protected static byte[] utfString;

    public static void main(String[] args) throws IOException {
        InitializeUTFClient();
    }

    public static void InitializeUTFClient() throws IOException {
        System.out.print("Ecrire un message: \n");
        reader = new BufferedReader(new InputStreamReader(System.in));

        do {
            try {
                String inputString = reader.readLine();
                SendToServer(inputString);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                IOUtil.close(reader);
            }
        } while (reader.ready());
    }

    private static void SendToServer(String message) throws IOException {
        port = 1337;
        socket = new Socket("127.0.0.1", port);

        try {
            writer = new OutputStreamWriter(socket.getOutputStream(), "UTF8");

            writer.write(message + "\n");
            writer.write("");
            writer.flush();

            socket.close();

        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtil.close(socket);
            IOUtil.close(writer);
        }
    }
}
